package com.shxzz.easy12306.services.ticket.toolkit;

import com.shxzz.easy12306.services.ticket.dto.domain.RouteDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * 站点计算工具类
 *
 * @author shxzz
 * @create 2024-03-05-23:53
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StationCalculateUtil {

    /**
     * 获取两站点间所有可能组合线路列表
     *
     * @param stationList  全部站点列表，按方向
     * @param startStation 起始站点
     * @param endStation   到达站点
     * @return 两站点间所有可能组合，按方向
     */
    public static List<RouteDTO> getThroughRouteList(List<String> stationList, String startStation, String endStation) {
        int startIndex = stationList.indexOf(startStation);
        int endIndex = stationList.indexOf(endStation);
        if(startIndex < 0 || endIndex < 0 || startIndex >= endIndex) return new ArrayList<>();

        // (n+1)*n/2
        List<RouteDTO> list = new ArrayList<>((endIndex - startIndex) * (endIndex - startIndex + 1) / 2);

        for (int i = startIndex; i < endIndex; i++) {
            for (int j = i+1; j <= endIndex; j++) {
                list.add(new RouteDTO(stationList.get(i), stationList.get(j)));
            }
        }
        return list;
    }

    /**
     * 获取两站点间所有可能组合线路列表
     *
     * @param stationList  全部站点列表，按方向
     * @param departure 起始站点
     * @param arrival   到达站点
     * @return 两站点间所有可能组合，按方向
     */
    public static List<RouteDTO> getTakeoutRouteList(List<String> stationList, String departure, String arrival) {
        int startIndex = stationList.indexOf(departure);
        int endIndex = stationList.indexOf(arrival);
        if(startIndex < 0 || endIndex < 0 || startIndex >= endIndex) return new ArrayList<>();
        // (n+1)*n/2
        List<RouteDTO> list = new ArrayList<>((endIndex - startIndex) * (endIndex - startIndex + 1) / 2);

        int len = stationList.size();
        // 出发点在购票区间前
        for (int i = 0; i < startIndex; i++) {
            for (int j = startIndex+1; j < len; j++) {
                list.add(new RouteDTO(stationList.get(i), stationList.get(j)));
            }
        }
        // 中间
        list.addAll(getThroughRouteList(stationList, departure, arrival));

        return list;
    }

}
